/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MobileNode;

import ippacket.Header;
import ippacket.Packet;
import mobileipsimulation.NodeInterface;

/**
 *
 * @author Rente
 */
public class MobileNode {

    private String MACAddress;
    private String ipAddress;
    private String homeAddress;
    private Receiver receiver;
    private Sender sender;
    private SolicitationThread st;
    private String careOfAddress;
    private String registeredAgent;
    private boolean home = true;
    private boolean hasSignal = false;
    private boolean connected = false;
    public static NodeInterface ni;

    public MobileNode(String mac, String ip, String home, NodeInterface noi) {
        ni = noi;
        this.MACAddress = mac;
        this.ipAddress = ip;
        this.homeAddress = home;
        careOfAddress = "49552";
        receiver = new Receiver(this);
        //st = new SolicitationThread(this);
        System.out.println("Node is running :) with ip " + this.getIpAddress());
    }

    public void outOfRangeAll (){
        new Sender(this, 49552, "localhost", "unregister;"+ipAddress);
        new Sender(this, 49553, "localhost", "unregister;"+ipAddress);
    }
    public void outOfRangeHA (){
        new Sender(this, 49552, "localhost", "unregister;"+ipAddress);
    }
    public void outOfRangeFA (){
        new Sender(this, 49553, "localhost", "unregister;"+ipAddress);
    }

    /**
     * @return the MACAddress
     */
    public String getMACAddress() {
        return MACAddress;
    }

    NodeInterface getNI() {
        return ni;
    }

    /**
     * @return the ipAddress
     */
    public String getIpAddress() {
        return ipAddress;
    }

    /**
     * @return the homeAddress
     */
    public String getHomeAddress() {
        return homeAddress;
    }

    public void sendMessage(String foreignAddress, String mensagem, int porto) {
        // System.out.println("sendMessage porto: " + porto);
        sender = new Sender(this, porto, foreignAddress, mensagem);
    }

    public void sendPacket(String address, String texto, int porto) {
        Packet p = new Packet();
        p.setContent(texto);
        Header h = new Header(address);
        p.addHeader(h);
        sendMessage(homeAddress, p.toString(), porto);
    }

    /**
     * @return the careOfAddress
     */
    public String getCareOfAddress() {
        return careOfAddress;
    }

    /**
     * @param careOfAddress the careOfAddress to set
     */
    public void setCareOfAddress(String careOfAddress) {
        this.careOfAddress = careOfAddress;
    }

    /**
     * @return the home
     */
    public boolean isHome() {
        return home;
    }

    /**
     * @param home the home to set
     */
    public void setHome(boolean home) {
        this.home = home;
    }

    /**
     * @param MACAddress the MACAddress to set
     */
    public void setMACAddress(String MACAddress) {
        this.MACAddress = MACAddress;
    }

    /**
     * @param ipAddress the ipAddress to set
     */
    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    /**
     * @param homeAddress the homeAddress to set
     */
    public void setHomeAddress(String homeAddress) {
        this.homeAddress = homeAddress;
    }

    /**
     * @return the hasSignal
     */
    public boolean isHasSignal() {
        return hasSignal;
    }

    /**
     * @param hasSignal the hasSignal to set
     */
    public void setHasSignal(boolean hasSignal) {
        this.hasSignal = hasSignal;
    }

    /**
     * @return the connected
     */
    public boolean isConnected() {
        return connected;
    }

    /**
     * @param connected the connected to set
     */
    public void setConnected(boolean connected) {
        this.connected = connected;
    }

    /**
     * @return the registeredAgent
     */
    public String getRegisteredAgent() {
        return registeredAgent;
    }

    /**
     * @param registeredAgent the registeredAgent to set
     */
    public void setRegisteredAgent(String registeredAgent) {
        this.registeredAgent = registeredAgent;
    }
}
